package com.hrt.datasender.datacollect.drive.modbus.tcp;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

import com.btcode.common.MyLog;
import com.btcode.log.ILog;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.NetExportDataModel;

/*
 * @author gislin
 *
 */
public class ModbusTCPMsgHandle extends SimpleChannelUpstreamHandler {

    private ILog log;
    private String ip;
    private String port;
    private String driveInstanceId;

    public ModbusTCPMsgHandle(String ip, String port, String driveInstanceId) {
        this.ip = ip;
        this.port = port;
        this.driveInstanceId = driveInstanceId;
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent msgEvent) throws Exception {

        if (log == null) {
            log = MyLog.getInstance().getLogger(getClass());
        }

        super.messageReceived(ctx, msgEvent);
        Object obj = msgEvent.getMessage();
        byte[] data = (byte[]) obj;
        // 网口获取的数据，添加到队列中去
        NetExportDataModel netExportDataModel = new NetExportDataModel(data, ip, port, driveInstanceId);
        MqKit.productMsgToNetExportMQ(netExportDataModel);
    }

}
